package com.ymatou.iapi.optrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.BizOperateLogEnum;
import com.ymatou.iapi.optrade.parameter.req.MarkOrderLockReq;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.MarkOrderLockCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.constant.ECode;
/**
*锁定解锁订单
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_MarkOrderLock {
	private static MarkOrderLockReq markOrderLockReq;
	private static MarkOrderLockCall markOrderLockCall;
	
	private static OrderShardingWapper database = new OrderShardingWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("锁定解锁订单");
	}

	@Before
	public void caseUp() {
		markOrderLockReq = new MarkOrderLockReq();
		markOrderLockCall = new MarkOrderLockCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(markOrderLockCall.getOpurl());
		database.activeDeadCommunicationsLink(database);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 *  校验订单 locked 和  订单业务操作记录表(order_biz_log)
	 */
	private void verifyOrderStatus(boolean flag) {

		long orderId = markOrderLockReq.getOrderId();

		// 订单表(order)
		OrderPo ymtOrders = database.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));

		Logger.verifyEquals(markOrderLockReq.getLockStatus() == 1,
				ymtOrders.getLocked(), "验证locked");

		//订单主状态操作日志表(order_status_log)
		OrderBizLogPo orderBizLogPo = database
				.selectOrderBizLogByOrderId(orderId);

		Logger.comment("验证订单业务操作记录表(order_biz_log)");

		String memo = String.format("原标记是:%s 新标记是:%s", flag,
				markOrderLockReq.getLockStatus() == 1);

		Logger.verifyEquals(memo, orderBizLogPo.getContent(), "验证数据库Content");

		Logger.verifyEquals(orderId, orderBizLogPo.getOrderId(), "验证数据库OrderId");

		Logger.verifyEquals(String.valueOf(orderId), orderBizLogPo.getBizNo(),
				"验证数据库BizNo");
		Logger.verifyEquals(BizOperateLogEnum.LOCK_ORDER.toString(),
				orderBizLogPo.getBizType(), "验证数据库BizType");
		Logger.verifyEquals(ymtOrders.getUserId(), orderBizLogPo.getOperator(),
				"验证数据库Operator");

	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderLock_001() {
		Logger.start(true, "锁定订单");
		try {
			//创建订单
			Logger.comment("前置数据准备开始");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			//参数
			markOrderLockReq.setOrderId(orderId);
			markOrderLockReq.setLockStatus(1);

			//发送请求
			markOrderLockCall.setData(markOrderLockReq);
			markOrderLockCall.callService();

			Logger.verifyEquals("200", markOrderLockCall.getString("code"),
					"验证code");

			Logger.verifyEquals(true, markOrderLockCall.getSuccess(),
					"验证success");

			//数据库验证
			verifyOrderStatus(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderLock_002() {
		Logger.start(true, "解锁订单");
		try {
			//创建订单
			Logger.comment("前置数据准备开始");

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			//参数
			markOrderLockReq.setOrderId(orderId);
			markOrderLockReq.setLockStatus(1);

			//发送请求
			markOrderLockCall.setData(markOrderLockReq);
			markOrderLockCall.callService();
			markOrderLockReq.setLockStatus(0);
			markOrderLockCall.callService();

			Logger.verifyEquals("200", markOrderLockCall.getString("code"),
					"验证code");

			Logger.verifyEquals(true, markOrderLockCall.getSuccess(),
					"验证success");

			//数据库验证
			verifyOrderStatus(true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderLock_003() {
		Logger.start(false, "订单号为空，锁定订单失败");
		try {
			//发送请求
			markOrderLockCall.setData(markOrderLockReq);
			markOrderLockCall.callService();

			Logger.verifyEquals("400", markOrderLockCall.getString("code"),
					"验证code");

			Logger.verifyEquals(false, markOrderLockCall.getSuccess(),
					"验证success");

			Logger.verifyIsContains(ECode.ERROR400_MSG_9,
					markOrderLockCall.getErrorMessage(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}